<?php

/**
 * H5 项目
 */
if (!class_exists('MaBox_H5')) {
    class MaBox_H5
    {
        private static $home;
        private static $contact;
        //加载
        public static function run()
        {
            //获取设置选项值
            $config = MaBox_Admin::get_seting('h5');

            //获取选项 - 首页
            $home =  MaBox_Admin::get_config($config, 'home');
            self::$home = $home;

            //获取选项 - 联系
            $contact =  MaBox_Admin::get_config($config, 'contact');
            self::$contact = $contact;

            //获取开关
            $switch =  MaBox_Admin::get_config($home, 'switch');
            if ($switch) {
                // 定义 REST API 端点（Endpoint）
                add_action('rest_api_init', array(__CLASS__, 'mytheme_register_rest_endpoints'));
            }
        }


        //接口网址http://localhost:10020/wp-json/carbon-fields/v1/h5-options
        public static function mytheme_register_rest_endpoints()
        {
            $request = 1;
            // Get theme options
            register_rest_route('carbon-fields/v1', 'h5-options', array(
                'methods' => 'GET',
                //'callback' => array(__CLASS__, 'mytheme_get_theme_options'),
                'callback' => array(__CLASS__, 'get_h5_options'),
                // 权限控制
                // 'permission_callback' => function () {
                //     return current_user_can('manage_options');
                // },
            ));
        }

        //返回选项值
        public static function mytheme_get_theme_options()
        {
            $fields = [
                'comm_h5_index_tone',
                'comm_h5_index_tone_cat',
                'comm_h5_index_category',
                'comm_h5_single_contact_title',
                'comm_h5_single_contact_one_title',
                'comm_h5_single_contact_one_content',
                'comm_h5_single_contact_two_title',
                'comm_h5_single_contact_two_content',
                'comm_h5_singel_featured_link',
                'comm_h5_singel_featured_logo',
                'comm_h5_singel_featured_msg',
            ];

            $options = [];
            foreach ($fields as $field) {
                $options[$field] = carbon_get_theme_option($field);
                //处理数组
                if ($field == "comm_h5_index_tone") {
                    //创建数组存储数据
                    $arr = [];
                    $sum = carbon_get_theme_option($field);
                    $arr = array_map(function ($obj) {
                        return $obj['id'];
                    }, $sum);
                    //转成数组
                    $intArray = array_map('intval', $arr);
                    $options[$field] = $intArray;
                }
            }
            return $options;
        }

        //返回选项值
        public static function get_h5_options($request)
        {
            //准备数据
            $home = self::$home;
            $contact = self::$contact;

            //首页
            $slide =  MaBox_Admin::get_config($home, 'slide');
            $slide_all =  MaBox_Admin::get_config($home, 'slide_all');
            $more =  MaBox_Admin::get_config($home, 'more');

            //联系
            $title =  MaBox_Admin::get_config($contact, 'title');
            $title_one =  MaBox_Admin::get_config($contact, 'title_one');
            $content_one =  MaBox_Admin::get_config($contact, 'content_one');
            $title_two =  MaBox_Admin::get_config($contact, 'title_two');
            $content_two =  MaBox_Admin::get_config($contact, 'content_two');
            $brand_link =  MaBox_Admin::get_config($contact, 'brand_link');
            $brand_logo =  MaBox_Admin::get_config($contact, 'brand_logo');
            $introduce =  MaBox_Admin::get_config($contact, 'introduce');

            $options['comm_h5_index_tone'] = $slide; //数字数组
            $options['comm_h5_index_tone_cat'] = $slide_all; //字符串
            $options['comm_h5_index_category'] = $more; //数字
            $options['comm_h5_single_contact_title'] = $title;
            $options['comm_h5_single_contact_one_title'] = $title_one;
            $options['comm_h5_single_contact_one_content'] = $content_one;
            $options['comm_h5_single_contact_two_title'] = $title_two;
            $options['comm_h5_single_contact_two_content'] = $content_two;
            $options['comm_h5_singel_featured_link'] = $brand_link;
            $options['comm_h5_singel_featured_logo'] = $brand_logo;
            $options['comm_h5_singel_featured_msg'] = $introduce;
            return $options;
        }
    } //end
}
